app.controller('tokenserviceMainCtrl', ['$rootScope','$scope', '$http', function( $rootScope,$scope, $http) {
        $scope.filterOptions = {
            filterText: "",
            useExternalFilter: true
        };
        $scope.totalServerItems = 0;
        $scope.pagingOptions = {
            pageSizes: [250, 500, 1000],
            pageSize: 250,
            currentPage: 1
        };
        $scope.setPagingData = function(data, page, pageSize) {
            var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
            $scope.myData = pagedData;
            $scope.totalServerItems = data.length;
            if (!$scope.$$phase) {
                $scope.$apply();
            }
        };
        $scope.getPagedDataAsync = function(pageSize, page, searchText) {
            setTimeout(function() {
                var data;
                $http.get('http://localhost:8083/client').success(function (data) {
                    if(data.resultFlag) {
                        data=data.data.list;
                        $scope.setPagingData(data, page, pageSize);
                    }else{
                        alert(data.reason);
                    }
                });


            }, 100);

        };

        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

        $scope.$watch('pagingOptions', function(newVal, oldVal) {
            if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
            }
        }, true);
        $scope.$watch('filterOptions', function(newVal, oldVal) {
            if (newVal !== oldVal) {
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
            }
        }, true);

        $scope.selectedItems=[];
        $scope.gridOptions =  angular.extend({}, $rootScope.gridOptions, {
            data: 'myData',
            enablePaging: true,
            multiSelect: true,
            enableRowSelection: true,
            showSelectionCheckbox: true,
            selectedItems:  $scope.selectedItems,
            showFooter: true,
            totalServerItems: 'totalServerItems',
            pagingOptions: $scope.pagingOptions,
            filterOptions: $scope.filterOptions,
            columnDefs: [{
                field: 'client_id',
                displayName: '客户ID',
                width: 200,
                sortable: true
            },{
                field: "client_secret",
                displayName: "客户秘钥",
                sortable: false
            },{
                field: "authorized_grant_types",
                displayName: "授权类型",
                sortable: false
            }, {
                field: "scope",
                displayName: "授权范围",
                width: 200,
                sortable: false
            }, {
                field: "access_token_validity",
                displayName: "token校验时间",
                sortable: false
            }, {
                field: "a",
                displayName: "操作",
                width: 200,
                sortable: false,
                cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()"><a href="#" class="btn btn-default btn-xs purple"><i class="fa fa-edit"></i> 编辑</a>&nbsp;<a href="#" class="btn btn-default btn-xs black"><i class="fa fa-trash-o"></i> Delete</a></div>'
            }]
        });
    }
]);